package com.ruoyi.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author hlf
 * @createTime 2025/2/6 16:16
 * @description
 */
@Configuration
@ConditionalOnProperty(name = "spring.rabbitmq.enabled", havingValue = "true", matchIfMissing = false)
public class RabbitMQConfig {

    //测试队列
    public static final String QUEUE_NAME = "my_queue";

    // 新增队列
    public static final String QUEUE_NAME_ARTICLE = "article_queue";

    // 新增集合队列
    public static final String QUEUE_NAME_ARTICLE_ALL = "article_queue_all";

    // 删除队列
    public static final String QUEUE_NAME_ARTICLE_DEL = "article_queue_del";

    // 交换机
    public static final String EXCHANGE_NAME = "my_exchange";


    public static final String ROUTING_KEY = "my_routing_key";
    public static final String ROUTING_KEY_ARTICLE = "routing_key_article";
    public static final String ROUTING_KEY_ARTICLE_ALL = "routing_key_article_all";
    public static final String ROUTING_KEY_ARTICLE_DEL = "routing_key_article_del";



    @Bean
    @Qualifier("lazyQueue")
    public Queue lazyQueue(){
        return QueueBuilder
                .durable(QUEUE_NAME)
                .lazy() // 开启Lazy模式
                .build();
    }

    @Bean
    @Qualifier("articleQueue")
    public Queue articleQueue(){
        return QueueBuilder
                .durable(QUEUE_NAME_ARTICLE)
                .lazy() // 开启Lazy模式
                .build();
    }

    @Bean
    @Qualifier("articleQueueAll")
    public Queue articleQueueAll(){
        return QueueBuilder
                .durable(QUEUE_NAME_ARTICLE_ALL)
                .lazy() // 开启Lazy模式
                .build();
    }

    @Bean
    @Qualifier("articleQueueDel")
    public Queue articleQueueDel(){
        return QueueBuilder
                .durable(QUEUE_NAME_ARTICLE_DEL)
                .lazy() // 开启Lazy模式
                .build();
    }

    @Bean
    @Qualifier("lazyExchange")
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME,true,false);
    }



    @Bean
    public Binding binding(@Qualifier("lazyQueue") Queue queue, @Qualifier("lazyExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    @Bean
    public Binding bindingArticle(@Qualifier("articleQueue") Queue queue, @Qualifier("lazyExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_ARTICLE);
    }


    @Bean
    public Binding bindingArticleAll(@Qualifier("articleQueueAll") Queue queue, @Qualifier("lazyExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_ARTICLE_ALL);
    }

    @Bean
    public Binding bindingArticleDel(@Qualifier("articleQueueDel") Queue queue, @Qualifier("lazyExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_ARTICLE_DEL);
    }

}
